﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.BIZ;
using Assessment.Management.SharedComponent.Entities;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.SharedComponent.Constants;
using System.Net;

namespace Assessment.Management.UI.Assessment
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SubjectParam paramsubject = new SubjectParam(FunctionType.GetListSubjects);
                MainController.Provider.Execute(paramsubject);
                ddlSubject.DataSource = paramsubject.listSubject;
                ddlSubject.DataBind();
                ddlSubject.Items.Insert(0, new ListItem("----------Chọn Môn----------", ""));
                ddlSubject.SelectedIndex = 0;
            }
        }
        string s;
        protected void btnLogin_Click(object sender, EventArgs e)
        {
           
            String strHostName = Dns.GetHostName();
            IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
            foreach (IPAddress ipaddress in iphostentry.AddressList)
            {
                s = ipaddress.ToString();
            }
            PollParam param = new PollParam(FunctionType.CheckConfig);
            Poll poll = new Poll();
            poll.Password = txtPassword.Text;
            param.Poll = poll;
            MainController.Provider.Execute(param);
            IPParam paramip = new IPParam(FunctionType.CheckIP);
            IPAddres ip = new IPAddres();
            ip.IPAddress = s;
            paramip.IPAddres = ip;
            MainController.Provider.Execute(paramip);
            IPParam paramids = new IPParam(FunctionType.CheckIDSubject);
            IPAddres ips = new IPAddres();
            ips.ConfigID = ddlSubject.SelectedValue;
            paramids.IPAddres = ips;
            MainController.Provider.Execute(paramids);

            if (param.Poll != null && paramip.IPAddres == null || (paramip.IPAddres != null && paramids.IPAddres == null))
            {
                HttpCookie password = new HttpCookie("password", txtPassword.Text);
                password.Expires = DateTime.Now.AddHours(4);
                Response.Cookies.Add(password);
                IPParam paramadd = new IPParam(FunctionType.CreateIP);
                IPAddres cl = new IPAddres();
                cl.IPAddress = s;
                cl.ConfigID = ddlSubject.SelectedValue;
                paramadd.IPAddres = cl;
                MainController.Provider.Execute(paramadd);
                Response.Redirect("Default.aspx");
            }
            else
            {
                lblCheck.Text = "<font color='red'>Sai thông tin tài khoản hoặc mật khẩu!</font>";
            }
        }
    }
}